package com.dream.inspect.modules.business.controller;

import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SuperviseStandardEntity;
import com.dream.inspect.modules.business.service.SuperviseStandardService;
import com.dream.inspect.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 督察问题标准表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:52
 */
@RestController
@CrossOrigin
@RequestMapping("business/supervisestandard")
public class SuperviseStandardController extends AbstractController {
    @Autowired
    private SuperviseStandardService superviseStandardService;

    /**
     * 列表一级
     */
    @RequestMapping("/firstOrderList")
    @RequiresPermissions("business:supervisestandard:list")
    public R firstOrderList(@RequestParam Map<String, Object> params) {
        List<SuperviseStandardEntity> list = superviseStandardService.findListByCodeAndLevel(params);
        return R.ok().put("superviseStandard", list);
    }

    /**
     * 列表树结构
     */
    @RequestMapping("/findAllList")
    @RequiresPermissions("business:supervisestandard:list")
    public R findAllList() {
        List<SuperviseStandardEntity> list = superviseStandardService.findAllListTree();
        return R.ok().put("superviseStandard", list);
    }
    /**
     * 列表树结构
     */
    @RequestMapping("/findAllListTree")
    @RequiresPermissions("business:supervisestandard:list")
    public R findAllListTree() {
        List<SuperviseStandardEntity> list = superviseStandardService.findAllListTree();
        return R.ok().put("list", list);
    }

    /**
     * 列表所有
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:supervisestandard:list")
    public R list() {
        ///分页
        /*PageUtils page = superviseStandardService.queryPage(params);
        return R.ok().put("page", page);*/
        List<SuperviseStandardEntity> list=superviseStandardService.findAllDesc();
        return R.ok().put("list", list);
    }
    /**
     * 列表所有 父id
     */
    @RequestMapping("/findListByParentId")
    @RequiresPermissions("business:supervisestandard:list")
    public R findListByParentId(@RequestParam Map<String, Object> params) {
        List<SuperviseStandardEntity> list = superviseStandardService.findStandardListByParentId(params);
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:supervisestandard:info")
    public R info(@PathVariable("id") Long id) {
        SuperviseStandardEntity superviseStandard = superviseStandardService.selectById(id);

        return R.ok().put("superviseStandard", superviseStandard);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:supervisestandard:save")
    public R save(@RequestBody SuperviseStandardEntity superviseStandard) {
        //@RequestBody SuperviseStandardEntity superviseStandard
        //SuperviseStandardEntity superviseStandard=new SuperviseStandardEntity();
        //superviseStandardService.insert(superviseStandard);
        superviseStandard.setCreateUser(getUserId());
        superviseStandard.setDeptId(getDeptId());
        superviseStandardService.addEntity(superviseStandard);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:supervisestandard:update")
    public R update(@RequestBody SuperviseStandardEntity superviseStandard) {
        ValidatorUtils.validateEntity(superviseStandard);
        if (superviseStandard.getDeptId() != getDeptId()) {
            return R.error();
        }
        superviseStandard.setUpdateUser(getUserId());
        superviseStandard.setUpdateTime(new Date());
        superviseStandardService.updateAllColumnById(superviseStandard);//全部更新

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("business:supervisestandard:update")
    public R updateStatus(@RequestBody SuperviseStandardEntity superviseStandard) {
        ValidatorUtils.validateEntity(superviseStandard);
        if (superviseStandard.getDeptId() != getDeptId()) {
            return R.error();
        }
        SuperviseStandardEntity superviseStandardEntity = new SuperviseStandardEntity();
        superviseStandardEntity.setUpdateUser(getUserId());
        superviseStandardEntity.setUpdateTime(new Date());
        superviseStandardEntity.setStatus(superviseStandard.getStatus());
        superviseStandardEntity.setId(superviseStandard.getId());
        superviseStandardService.updateById(superviseStandard);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:supervisestandard:delete")
    // public R delete(@RequestBody Long[] ids) {
    public R delete(@RequestParam Map<String, Object> params) {

        //暂时不提供删除
        //superviseStandardService.deleteBatchIds(Arrays.asList(ids));
        SuperviseStandardEntity superviseStandard = new SuperviseStandardEntity();
        superviseStandard.setId(Long.valueOf(params.get("id") + ""));
        superviseStandard.setDeleteFlag("1");
        superviseStandard.setUpdateUser(getUserId());
        superviseStandard.setUpdateTime(new Date());
        superviseStandardService.updateById(superviseStandard);//全部更新
        return R.ok();
    }

    /**
     * 获取新增初始信息
     */
    @RequestMapping("/getNextObjectInfo")
    @RequiresPermissions("business:supervisestandard:save")
    public R getNextObjectInfo(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        //暂时不提供删除
        Map<String, Object> superviseStandard = superviseStandardService.findNextInfo(params);

        return R.ok().put("superviseStandard", superviseStandard);
    }

    /**
     * 获取问题统计信息
     */
    @RequestMapping("/getQuestionCount")
    @RequiresPermissions("business:supervisestandard:info")
    public R getQuestionCount(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        //暂时不提供删除
        Map<String, Object> superviseStandard = superviseStandardService.getQuestionCount(params);

        return R.ok().put("countMap", superviseStandard);
    }

}
